module app.controller.UploadController;

import hunt.framework;
import comm.UploadToken;
import hunt.serialization;
import comm.HttpResult;
import hunt.util.MimeTypeUtils;

class UploadController : Controller
{
    mixin MakeController;

    @Action
    string test()
    {
        return view.render("upload");
    }

    @Action
    JsonResponse upload(string token)
    {
        string message;

        import std.stdio;

        auto result = new ResponseResult!UploadedFile;

        if (request.input("token", null) is null)
        {
            result.code = 500001;
            result.message = "Missing token parameter.";
            return new JsonResponse(result);
        }

        // TODO: 使用真的 token 校验取消这段代码注释
        // UploadToken uploadToken = toObject!UploadToken(Application.instance().redis().get("upload:token:" ~ request.input("token")));
        // if (uploadToken is null || uploadToken.user_id.length == 0)
        // {
        //     result.code = 500001;
        //     result.message = "Invalid token.";
        //     return new JsonResponse(result);
        // }

        // writeln("User ID: ", uploadToken.user_id);

        if (request.hasFile("file"))
        {
            auto file = request.file("file");

            if (file.isValid())
            {
                // File save path: file.path()
                // Origin name: file.originalName()
                // File extension: file.extension()
                // File mimetype: file.mimeType()

                writeln("File info: ", file.path(), " , ", file.originalName(), " , ", file.extension(), " , ", file.mimeType());

                // string fileName = uploadToken.user_id ~ "_" ~ uploadToken.token ~ ".file";
                // string fileName = rndStr(32);
                string fileName = request.input("token");
                writeln("New file name: ", fileName);

                try
                {
                    file.store(Application.instance().config().upload.path ~ "/" ~ fileName);

                    UploadedFile uploadedFile = new UploadedFile;
                    uploadedFile.filename = fileName;
                    uploadedFile.url = "http://files.openchat.mobi/attachments/" ~ fileName;
                    uploadedFile.mimetype = file.mimeType();
                    // uploadedFile.user_id = uploadToken.user_id;
                    uploadedFile.user_id = "0";
                    uploadedFile.time = time();

                    result.data = uploadedFile;

                    Application.instance().redis().del("upload:token:" ~ request.input("token"));

                    return new JsonResponse(result);
                }
                catch(Exception e)
                {
                    result.message = e.msg;
                }
            }
            else
            {
                result.message = "file is not valid";
            }
        }
        else
        {
            result.message = "not get this file";
        }
    
        result.code = 500001;
        return new JsonResponse(result);
    }

    @Action
    JsonResponse generateToken()
    {
        string token = rndStr(32);

        auto uploadToken = new UploadToken;
        uploadToken.user_id = "10001";
        uploadToken.token = token;
        uploadToken.time = 1234567890;

        Application.instance().redis().set("upload:token:" ~ token, toJson(uploadToken).toString());

        auto result = new ResponseResult!UploadToken;
        result.data = uploadToken;
        
        return new JsonResponse(result);
    }
}
